function countText( field ) {
	var lblName = "lbl"+field.name;
	if(field.getAttribute('maxlength')!=null){
	var maxlimit = field.getAttribute('maxlength');
	 if ( field.value.length > maxlimit ) {
	  field.value = field.value.substring( 0, maxlimit );
	  field.blur();
	  field.focus();
	  return false;
	 } else {
	  document.getElementById(lblName).innerHTML = "<font class='sectionheaderadditionaltextalert'>chars left :"+ (maxlimit - field.value.length )+ "</font>";
	 }
	}
}


	function submitForm(btn) {
	if(validInputs()){
		btn.disabled = true;
		btn.value = "Processing...";
		document.getElementById("serviceForm").submit();
		}
	}

function setMsg(mmsgs){
	try{
		document.getElementById("messages").innerHTML=mmsgs;
	}catch(e){
		document.getElementById("msgJsp").innerHTML=mmsgs;	
	}
}

function clearMsg(){
	toView="";
	setMsg(toView);
}

function makeW(str){ return "<div><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span><font class='reviewalertmessages'>"+str+"</font></div>"; }

var toView="";

function validInputs(){
	var isvalid=true;	
	try{
		clearMsg();
		
if(stringFields!=null){
	for(var i=0; i <  stringFields.length; i++){
		if(!validateNotEmpty(getVal(stringFields[i]))){
			isvalid=false;
			toView+=makeW("Invalid "+stringFields[i]);
		}	
	}
}

if(dateFields!=null){	
	for(var i=0; i <  dateFields.length; i++){
		if(!validateUSDate(getVal(dateFields[i]))){
			isvalid=false;
			toView+=makeW("Invalid "+dateFields[i]);
		}	
	}
}

if(intFields!=null){	
	for(var i=0; i <  intFields.length; i++){
		if(!validateInteger(getVal(intFields[i]))){
			isvalid=false;
			toView+=makeW("Invalid "+intFields[i]);
		}	
	}
}

if(timeFields!=null){	
	for(var i=0; i <  timeFields.length; i++){
		if( (!validateNotEmpty(getVal(timeFields[i]))) ||(!isValidTime(getVal(timeFields[i])))){
			isvalid=false;
			toView+=makeW("Invalid "+timeFields[i]);
		}	
	}
}

if(emailFields!=null){	
	for(var i=0; i <  emailFields.length; i++){
		if( (!validateNotEmpty(getVal(emailFields[i]))) ||(!validateEmail(getVal(emailFields[i])))){
			isvalid=false;
			toView+=makeW("Invalid "+emailFields[i]);
		}	
	}
}

if(doubleFields!=null){	
	for(var i=0; i <  doubleFields.length; i++){
		if( (!validateNotEmpty(getVal(doubleFields[i]))) ||(!validateNumeric(getVal(doubleFields[i])))){
			isvalid=false;
			toView+=makeW("Invalid "+doubleFields[i]);
		}	
	}
}

	}catch(e){
		isvalid=false;
		toView+=makeW(e);
	}
	
	if(toView.length > 0){		
		setMsg('<div class="ui-widget" width="80%"> '+
				'<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">'+ 
					'<strong>Validation Error(s):</strong> '+toView+' '+
				'</div>'+
			'</div>');
	}
	return isvalid;
}

function getVal(eid){
return (document.getElementById(eid)!=null) ?  document.getElementById(eid).value : "undefined";	
}

function removeByValue(arr, val) {
    for(var i=0; i<arr.length; i++) {
        if(arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }
}

function parentExists()
{
 return (parent.location == window.location)? false : true;
}

function makeInputElement(name, size, maxlength, type, value){
	var myelement = document.createElement('input');
	myelement.setAttribute('id', name);
	myelement.setAttribute('name', name);
	myelement.setAttribute('type', type);
	myelement.setAttribute('value', value);
	if(maxlength >0){
		myelement.setAttribute('maxlength', maxlength);
		}
	
	if(size >0){
		myelement.setAttribute('size', size);
		}
	return myelement;
}

function makeExpandableTextArea(name, maxlength, cols, rows){
	var myelement = document.createElement('textarea');
	myelement.setAttribute('id', name);
	myelement.setAttribute('name', name);
	myelement.setAttribute('class', 'textarea');
	myelement.setAttribute('cols', cols);
	myelement.setAttribute('rows', rows);
	myelement.setAttribute('style', 'overflow-x: visible; overflow-y: visible; width:100%;');
	
	if(maxlength >0){
		myelement.setAttribute('maxlength', maxlength);
		}
	return myelement;
}

function showMore(moreId){
	try{
		$("#"+moreId).show('slow');
		$("#link"+moreId).hide('fast');
	}catch(t){
		document.getElementById(moreId).style.display="";
		document.getElementById("link"+moreId).style.display="none";
	}
}

var rowStyle=new Array("listtableroweven", "listtablerowodd");

function getRowStyle(r){
	return rowStyle[r % 2];
}

function GetXmlHttpObject()
{
    var xmlHttp = null;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function trim11 (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function validateInteger( strValue ) {
	/***************************************************************************
	 * DESCRIPTION: Validates that a string contains only valid integer number.
	 * 
	 * PARAMETERS: strValue - String to be tested for validity
	 * 
	 * RETURNS: True if valid, otherwise false.
	 **************************************************************************/
	  var objRegExp  = /(^-?\d\d*$)/;

	  // check for integer characters
	  return objRegExp.test(strValue);
	}

	function validateNotEmpty( strValue ) {
	/***************************************************************************
	 * DESCRIPTION: Validates that a string is not all blank (whitespace)
	 * characters.
	 * 
	 * PARAMETERS: strValue - String to be tested for validity
	 * 
	 * RETURNS: True if valid, otherwise false.
	 **************************************************************************/
	   var strTemp = strValue;
	   strTemp = trimAll(strTemp);
	   if(strTemp.length > 0){
	     return true;
	   }
	   return false;
	}
	
	function trimAll( strValue ) {
		/***********************************************************************
		 * DESCRIPTION: Removes leading and trailing spaces.
		 * 
		 * PARAMETERS: Source string from which spaces will be removed;
		 * 
		 * RETURNS: Source string with whitespaces removed.
		 **********************************************************************/
		 var objRegExp = /^(\s*)$/;
		    // check for all spaces
		    if(objRegExp.test(strValue)) {
		       strValue = strValue.replace(objRegExp, '');
		       if( strValue.length == 0)
		          return strValue;
		    }

		   // check for leading & trailing spaces
		   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
		   if(objRegExp.test(strValue)) {
		       // remove leading and trailing whitespace characters
		       strValue = strValue.replace(objRegExp, '$2');
		    }
		  return strValue;
		}
	
	function validateUSDate( strValue ) {
		/***********************************************************************
		 * DESCRIPTION: Validates that a string contains only valid dates with 2
		 * digit month, 2 digit day, 4 digit year. Date separator can be ., -,
		 * or /. Uses combination of regular expressions and string parsing to
		 * validate date. Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
		 * 
		 * PARAMETERS: strValue - String to be tested for validity
		 * 
		 * RETURNS: True if valid, otherwise false.
		 * 
		 * REMARKS: Avoids some of the limitations of the Date.parse() method
		 * such as the date separator character.
		 **********************************************************************/
		var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
		 
		  // check to see if in correct format
		  if(!objRegExp.test(strValue))
		    return false; // doesn't match pattern, bad date
		  else{
		    var strSeparator = strValue.substring(2,3) 
		    var arrayDate = strValue.split(strSeparator); 
		    // create a lookup for months not equal to Feb.
		    var arrayLookup = { '01' : 31,'03' : 31, 
		                        '04' : 30,'05' : 31,
		                        '06' : 30,'07' : 31,
		                        '08' : 31,'09' : 30,
		                        '10' : 31,'11' : 30,'12' : 31}
		    var intDay = parseInt(arrayDate[1],10); 

		    // check if month value and day value agree
		    if(arrayLookup[arrayDate[0]] != null) {
		      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
		        return true; // found in lookup table, good date
		    }
		    
		    // check for February (bugfix 20050322)
		    // bugfix for parseInt kevin
		    // bugfix biss year O.Jp Voutat
		    var intMonth = parseInt(arrayDate[0],10);
		    if (intMonth == 2) { 
		       var intYear = parseInt(arrayDate[2]);
		       if (intDay > 0 && intDay < 29) {
		           return true;
		       }
		       else if (intDay == 29) {
		         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
		             (intYear % 400 == 0)) {
		              // year div by 4 and ((not div by 100) or div by 400)
						// ->ok
		             return true;
		         }   
		       }
		    }
		  }  
		  return false; // any other values, bad date
		}

	
	function isValidTime(value) {
		   var hasMeridian = false;
		   var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
		   if (!re.test(value)) { return false; }
		   if (value.toLowerCase().indexOf("p") != -1) { hasMeridian = true; }
		   if (value.toLowerCase().indexOf("a") != -1) { hasMeridian = true; }
		   var values = value.split(":");
		   if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) { return false; }
		   if (hasMeridian) {
		      if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) { return false; }
		   }
		   if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) { return false; }
		   if (values.length > 2) {
		      if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) { return false; }
		   }
		   return true;
		}
	
	
	function validateEmail( strValue) {
		/************************************************
		DESCRIPTION: Validates that a string contains a
		  valid email pattern.

		 PARAMETERS:
		   strValue - String to be tested for validity

		RETURNS:
		   True if valid, otherwise false.

		REMARKS: Accounts for email with country appended
		  does not validate that email contains valid URL
		  type (.com, .gov, etc.) or valid country suffix.
		*************************************************/
		var objRegExp  =/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;

		  //check for valid email
		  return objRegExp.test(strValue);
		}
	
	function  validateNumeric( strValue ) {
		/*****************************************************************
		DESCRIPTION: Validates that a string contains only valid numbers.

		PARAMETERS:
		   strValue - String to be tested for validity

		RETURNS:
		   True if valid, otherwise false.
		******************************************************************/
		  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

		  //check for numeric characters
		  return objRegExp.test(strValue);
		}